package com.yoki.loginview;

import android.app.Activity;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v7.app.AppCompatActivity;
import android.view.View;
import android.widget.AdapterView;
import android.widget.GridView;
import android.widget.SimpleAdapter;
import android.widget.Toast;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

public class GridViewActivity extends AppCompatActivity implements AdapterView.OnItemClickListener {

    private GridView gridView;
    String FruitName[] = {"苹果","香蕉","樱桃","葡萄","芒果","桔子","梨","菠萝","草莓","西瓜" };
    int image[] = {R.drawable.apple_pic,R.drawable.banana_pic,R.drawable.cherry_pic,R.drawable.grape_pic,R.drawable.mango_pic,R.drawable.orange_pic,R.drawable.pear_pic,R.drawable.pineapple_pic,R.drawable.strawberry_pic,R.drawable.watermelon_pic};
    String[] from = {"img","name"};
    int[] to = {R.id.imageView,R.id.fruit};

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_gridview);
        gridView = findViewById(R.id.GridView);
        gridView.setOnItemClickListener(this);
        List<Map<String,Object>> imageList = new ArrayList<Map<String, Object>>();
        for(int i=0;i<FruitName.length;i++)
        {
            Map<String,Object> map = new HashMap<String, Object>();
            map.put("img",image[i]);
            map.put("name",FruitName[i]);
            imageList.add(map);
        }

        SimpleAdapter adapter = new SimpleAdapter(this,imageList,R.layout.grid_one_item,from,to);
        gridView.setAdapter(adapter);
    }

    @Override
    public void onItemClick(AdapterView<?> adapterView, View view, int i, long l) {
        Toast.makeText(this,"点击了"+FruitName[i],Toast.LENGTH_SHORT).show();
    }
}
